﻿using System;
using System.Collections.Generic;
using System.Text;
using TM.QWeibo.SDK.Objects;

namespace TM.QWeibo.SDK.API
{
    /// <summary>
    /// 与关系链相关的接口实现
    /// </summary>
    public class Friends
        : RequestBase
    {
        /// <summary>
        /// 根据授权对象实例化
        /// </summary>
        /// <param name="oauth"></param>
        public Friends(OAuth oauth)
            : base(oauth)
        { }

        Action<ResponseData<UserInfo>> Callback { get; set; }

        #region 我的听众列表

        /// <summary>
        /// 获取用户本人最新n个听众列表
        /// </summary>
        /// <param name="reqnum">每次请求记录的条数（1-30条）</param>
        /// <param name="startindex">起始位置（第一页填0，继续向下翻页：填：【reqnum*（page-1）】）</param>
        /// <param name="callback"></param>
        public void GetFanslist(int reqnum, int startindex, Action<ResponseData<UserInfo>> callback)
        {
            this.Callback = callback;
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("reqnum", reqnum);
            parameters.Add("startindex", startindex);
            GetData("http://open.t.qq.com/api/friends/fanslist", parameters, GetFansListEnd);
        }

        /// <summary>
        /// 结束回调
        /// </summary>
        /// <param name="json"></param>
        private void GetFansListEnd(string json)
        {
            if (Callback != null)
            {
                var obj = Util.JsonToObject<ResponseData<UserInfo>>(json);
                Callback(obj);
            }
        }

        #endregion

        #region 我收听的人列表

        /// <summary>
        /// 获取用户本人已收听的最新n个人列表。
        /// </summary>
        /// <param name="reqnum">每次请求记录的条数（1-30条）</param>
        /// <param name="startindex">起始位置（第一页填0，继续向下翻页：填：【reqnum*（page-1）】）</param>
        /// <param name="callback"></param>
        public void GetIdollist(int reqnum, int startindex, Action<ResponseData<UserInfo>> callback)
        {
            this.Callback = callback;
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("reqnum", reqnum);
            parameters.Add("startindex", startindex);
            parameters.Add("install", "0");
            GetData("http://open.t.qq.com/api/friends/idollist", parameters, GetFansListEnd);
        }

        /// <summary>
        /// 我的听众的简单资料
        /// </summary>
        /// <param name="reqnum"></param>
        /// <param name="startindex"></param>
        /// <param name="callback"></param>
        public void GetSimpleIdollist(int reqnum, int startindex, Action<ResponseData<UserInfo>> callback)
        {
            this.Callback = callback;
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("reqnum", 50);
            parameters.Add("startindex", startindex);
            parameters.Add("install", "0");
            GetData("http://open.t.qq.com/api/friends/idollist_s", parameters, GetFansListEnd);
        }

        #endregion


        #region 特别收听列表.
        /// <summary>
        /// 采用默认API请求地址获取用户本人的特别收听列表。
        /// </summary>
        /// <param name="reqnum">每次请求记录的条数（1-30条）</param>
        /// <param name="startindex">起始位置（第一页填0，继续向下翻页：填：【reqnum*（page-1）】）</param>
        /// <returns>获取用户本人的特别收听列表</returns>
        /// <param name="callback">回调</param>
        public void GetSpeciallist(int reqnum, int startindex,Action<ResponseData<UserInfo>> callback)
        {
            this.Callback = callback;
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("reqnum", reqnum);
            parameters.Add("startindex", startindex);
            GetData("http://open.t.qq.com/api/friends/speciallist", parameters,GetFansListEnd);
        }
        #endregion

        #region 收听

        /// <summary>
        /// 收听回调
        /// </summary>
        Action<ResponseDataBase<string>> FriendActCallback { get; set; }

        /// <summary>
        /// 采用默认API请求地址收听某个用户
        /// </summary>
        /// <param name="name">他人的帐户名</param>
        public void Add(string name,Action<ResponseDataBase<string>> callback)
        {
            this.FriendActCallback = callback;
            Parameters parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            this.PostData("http://open.t.qq.com/api/friends/add", parameters, FriendActionEnd);
        }

        /// <summary>
        /// 采用默认API请求地址取消收听某个用户
        /// </summary>
        /// <param name="name">他人的帐户名</param>
        public void Delete(string name, Action<ResponseDataBase<string>> callback)
        {
            this.Delete("http://open.t.qq.com/api/friends/del", name,callback);
        }
        /// <summary>
        /// 取消收听某个用户
        /// </summary>
        /// <param name="requestUrl">API请求地址</param>
        /// <param name="name">他人的帐户名</param>
        private void Delete(string requestUrl, string name, Action<ResponseDataBase<string>> callback)
        {
            this.FriendActCallback = callback;
            Parameters parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            this.PostData(requestUrl, parameters, FriendActionEnd);
        }

        private void FriendActionEnd(string json) {
            var rs = Util.JsonToObject<ResponseDataBase<string>>(json);
            if (FriendActCallback != null) {
                FriendActCallback(rs);
            }
        }

        #endregion

        #region 特别收听某个用户
        /// <summary>
        /// 采用默认API请求地址特别收听某个用户
        /// </summary>
        /// <param name="name">他人的帐户名</param>
        /// <returns>操作结果</returns>
        public void AddSpecial(string name, Action<ResponseDataBase<string>> callback)
        {
            this.FriendActCallback = callback;
            Parameters parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            this.PostData("http://open.t.qq.com/api/friends/addspecail", parameters, FriendActionEnd);
        }
        #endregion

        #region 取消特别收听某个用户
        /// <summary>
        /// 采用默认API请求地址取消特别收听某个用户
        /// </summary>
        /// <param name="name">他人的帐户名</param>
        /// <returns>操作结果</returns>
        public void DeleteSpecial(string name, Action<ResponseDataBase<string>> callback)
        {
            this.FriendActCallback = callback;
            Parameters parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            this.PostData("http://open.t.qq.com/api/friends/delspecial", parameters, FriendActionEnd);
        }
        #endregion

        #region 获取其他用户听众列表

        /// <summary>
        /// 获取其他用户听众列表
        /// </summary>
        /// <param name="name">用户帐户名</param>
        /// <param name="reqnum">每次请求记录的条数（1-30条）</param>
        /// <param name="startindex">起始位置（第一页填0，继续向下翻页：填：【reqnum*（page-1）】）</param>
        /// <param name="callback">回调</param>
        public void GetUserFanslist(string name, int reqnum, int startindex, Action<ResponseData<UserInfo>> callback)
        {
            this.Callback = callback;
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            parameters.Add("reqnum", reqnum);
            parameters.Add("startindex", startindex);
            GetData("http://open.t.qq.com/api/friends/user_fanslist", parameters, GetFansListEnd);
        }

        #endregion

        #region 获取其他用户收听的人列表

        /// <summary>
        /// 采用默认API请求地址获取其他用户收听的人列表
        /// </summary>
        /// <param name="name">用户帐户名</param>
        /// <param name="reqnum">每次请求记录的条数（1-30条）</param>
        /// <param name="startindex">起始位置（第一页填0，继续向下翻页：填：【reqnum*（page-1）】）</param>
        /// <param name="callback"></param>
        public void GetUserIdollist(string name, int reqnum, int startindex, Action<ResponseData<UserInfo>> callback)
        {
            this.Callback = callback;
            var parameters = new Parameters();
            parameters.Add("format", this.ResponseDataFormat.ToString().ToLower());
            parameters.Add("name", name);
            parameters.Add("reqnum", reqnum);
            parameters.Add("startindex", startindex);
            GetData("http://open.t.qq.com/api/friends/user_idollist", parameters, GetFansListEnd);
        }

        #endregion

    }
}
